package com.itheima.d3_thread_safe;

import static java.lang.Thread.sleep;

/**
 * 目标：模拟线程安全问题。
 */
public class ThreadTest {


    public static void main(String[] args) {

//        myRunnable myr=new myRunnable();

        Thread thread1=new Thread(new myRunnable(),"窗口1");
        Thread thread2=new Thread(new myRunnable(),"窗口2");
        Thread thread3=new Thread(new myRunnable(),"窗口3");

//        Thread thread1=new Thread(myr,"窗口1");
//        Thread thread2=new Thread(myr,"窗口2");
//        Thread thread3=new Thread(myr,"窗口3");

        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class myRunnable implements Runnable{
//    private static int tickets=100;
private static int tickets=100;
    @Override
    public void run() {
        while(true){
            synchronized (myRunnable.class){
                if(tickets<=0){
                    break;
                }else {

                    tickets--;
                    System.out.println(Thread.currentThread().getName()+"正在卖票，还剩"+tickets+"张");
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                }
            }

        }
    }
}


